package cn.tedu._05mvcboot02.controller;

import cn.tedu._05mvcboot02.base.response.JsonResult;
import cn.tedu._05mvcboot02.mapper.UserMapper;
import cn.tedu._05mvcboot02.pojo.dto.InsertDTO;
import cn.tedu._05mvcboot02.pojo.entity.User;
import cn.tedu._05mvcboot02.pojo.vo.UserListVO;
import cn.tedu._05mvcboot02.pojo.vo.UserVO;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;

import java.util.Date;
import java.util.List;

@RestController
@RequestMapping("/v1/users")
@Api(tags = "用户管理模块")
public class UserController {
    @Autowired
    private UserMapper userMapper;

    @PostMapping("insert")
    @ApiOperation("添加用户")
    @ApiImplicitParam(name = "insertDTO", value = "添加用户数据", required = true, dataType = "InsertDTO")
    public JsonResult insertUser(InsertDTO insertDTO) {
        User user = new User();
        BeanUtils.copyProperties(insertDTO, user);
        user.setCreated(new Date());
        userMapper.insertUser(user);
        return JsonResult.ok();
    }
    @GetMapping("userList")
    @ApiOperation("查询用户列表")
    public JsonResult userList(){
        return JsonResult.ok(userMapper.userList());
    }
    @GetMapping("delete")
    @ApiOperation("删除用户")
    @ApiImplicitParam(name = "id", value = "用户id", required = true, dataType = "Long")
    public JsonResult deleteUser(Long id){
        userMapper.deleteUser(id);
        return JsonResult.ok();
    }

    @PostMapping("update")
    @ApiOperation("修改用户信息")
    @ApiImplicitParam(name = "user", value = "用户信息", required = true, dataType = "User")
    public JsonResult updateUser(User user){
        return JsonResult.ok(userMapper.updateUser(user));
    }
    @GetMapping("selectById/{uid}")
    @ApiOperation("查询用户信息")
    public JsonResult selectById(@PathVariable Long uid){
        return JsonResult.ok(userMapper.selectById(uid));
    }
}
